package cn.edu.guet.tts.controller;

import cn.edu.guet.tts.common.Result;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import java.net.URLEncoder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.edu.guet.tts.entity.TbOrder;
import cn.edu.guet.tts.service.ITbOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.guet.tts.service.ITbTicketService;
import cn.edu.guet.tts.entity.TbTicket;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 车票管理表 前端控制器
 * </p>
 *
 * @author test
 * @since 
 */
@RestController
@RequestMapping("/tbTicket")
public class TbTicketController {

    private static final Logger log = LoggerFactory.getLogger(TbTicketController.class);
    @Resource
    private ITbTicketService tbTicketService;

    @Resource
    private ITbOrderService tbOrderService;

    /**
     * 新增或更新车票
     * @param tbTicket
     * @param userId
     * @param nickname
     * @return
     */
    @Transactional
    @PostMapping
    public Result save(@RequestBody TbTicket tbTicket, Integer userId, String nickname) {
        Integer id = tbTicket.getId();
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = now.format(formatter);
        TbOrder tbOrder = new TbOrder();
        tbOrder.setTicketId(id);
        tbOrder.setOrderTime(formattedTime); // 赋值当前时间字符串
        tbOrder.setFullname(nickname);
        tbOrder.setUserId(userId);
        tbOrder.setTrainId(tbTicket.getTrainId());
        tbOrder.setOrderStatus("已支付");
        log.info("创建或更新订单信息：{}", tbOrder);
        tbOrderService.saveOrUpdate(tbOrder);
        log.info("创建或更新订单车票：{}", tbTicket);
        tbTicketService.saveOrUpdate(tbTicket);
        return Result.success();
    }

    /**
     * 根据车票id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        tbTicketService.removeById(id);
        return Result.success();
    }

    /**
     * 根据车票id批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        tbTicketService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 查询所有车票
     * @return
     */
    @GetMapping
    public Result findAll() {
        return Result.success(tbTicketService.list());
    }

    /**
     * 根据车票id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(tbTicketService.getById(id));
    }

    /**
     * 分页
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<TbTicket> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.eq("train_id", name);
        }
        return Result.success(tbTicketService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

